package ru.spb.nicetu.schoolproject.gui;

import ru.spb.nicetu.schoolproject.data.*;

import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;

/**
 * Created by dchernyshov on 25.04.15.
 */
public class LessonCard extends CardTemplate<Lesson, Subject> {
    protected JComboBox classComboBox;
    protected JComboBox subjectComboBox;
    protected JComboBox teacherComboBox;
    protected JComboBox dayComboBox;
    protected JFormattedTextField lessonNumField;

    protected Box vbox;
    protected Box box1;
    protected Box box2;
    protected Box box3;
    protected Box box4;
    protected Box box5;

    public LessonCard() {
        super(Subject.class, "Урок", false, new Lesson());
        initComboBox();
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
        setSize(250, 200);
    }

    public LessonCard(Lesson lesson) {
        super(Subject.class, "Редактирование урока", true, lesson);
        initComboBox();
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
        classComboBox.setSelectedItem(lesson.getClazz());
        subjectComboBox.setSelectedItem(lesson.getSubject());
        teacherComboBox.setSelectedItem(lesson.getTeacher());
        dayComboBox.setSelectedItem(lesson.getDayOfWeek());
        lessonNumField.setValue(lesson.getLessonNum());
    }

    @Override
    public boolean checkCard() {
        if (lessonNumField.getText().isEmpty()) {
            errorDialog("Введите номер урока");
            return false;
        }
        return true;
    }

    @Override
    public void inputValue(Lesson object) {
        object.setClazz((SchoolClass)classComboBox.getSelectedItem());
        object.setSubject((Subject)subjectComboBox.getSelectedItem());
        object.setTeacher((Teacher)teacherComboBox.getSelectedItem());
        Number lesson = (Number)lessonNumField.getValue();
        object.setLessonNum(lesson.intValue());
        object.setDayOfWeek((DayOfWeek)dayComboBox.getSelectedItem());
    }

    public void initComboBox() {
        classComboBox = new JComboBox(new ComboBoxData(SchoolClass.class).getComboArray());
        classComboBox.setMaximumSize(classComboBox.getMinimumSize());
        subjectComboBox = new JComboBox(new ComboBoxData(Subject.class).getComboArray());
        subjectComboBox.setMaximumSize(subjectComboBox.getMinimumSize());
        teacherComboBox = new JComboBox(new ComboBoxData(Teacher.class).getComboArray());
        teacherComboBox.setMaximumSize(teacherComboBox.getMinimumSize());
        dayComboBox = new JComboBox(new ComboBoxData(DayOfWeek.class).getComboArray());
        dayComboBox.setMaximumSize(dayComboBox.getMinimumSize());

    }

    public Box createForthHorizontalBox() {
        Box box = Box.createHorizontalBox();
        JLabel label = new JLabel("№ урока");
        box.add(label);
        box.add(Box.createHorizontalStrut(10));
        lessonNumField = new JFormattedTextField(NumberFormat.getIntegerInstance());
        lessonNumField.setColumns(2);
        lessonNumField.setMaximumSize(lessonNumField.getPreferredSize());
        box.add(lessonNumField);
        box.add(Box.createHorizontalGlue());
        return box;
    }

    public void createBoxes() {
        box1 = createHorizontalBox("Класс:", SchoolClass.class, classComboBox);
        box2 = createHorizontalBox("Предмет:", Subject.class, subjectComboBox);
        box3 = createHorizontalBox("Учитель:", Teacher.class, teacherComboBox);
        box5 = createHorizontalBox("День недели:", Teacher.class, dayComboBox);
        box4 = createForthHorizontalBox();
    }

    public void addBoxesToVerticalBox() {
        vbox = Box.createVerticalBox();
        vbox.add(box1);
        vbox.add(Box.createVerticalStrut(10));
        vbox.add(box2);
        vbox.add(Box.createHorizontalStrut(10));
        vbox.add(box3);
        vbox.add(Box.createHorizontalStrut(10));
        vbox.add(box4);
        vbox.add(Box.createHorizontalStrut(10));
        vbox.add(box5);
    }

    public Box createVerticalBox() {
        createBoxes();
        addBoxesToVerticalBox();
        vbox.add(Box.createHorizontalStrut(10));
        vbox.add(createLastHorizontalBox());
        return vbox;
    }
}
